'use strict';

const Service = require('egg').Service;

class Members extends Service {
	async list({
		offset = 0,
		limit = 10
	}) {
		return this.ctx.model.Member.findAndCountAll({
			offset,
			limit,
			order: [
				['created_at', 'desc'],
				['id', 'desc']
			],
		});
	}
	//新增用户
	async save(data) {
		const user = await this.ctx.model.Member.findOne({
			where: {
				user_name: data.user_name
			}
		})
		if (!user) {
			const newUser = await this.ctx.model.Member.create(data);
			console.log(newUser);
			return {
				code: 200,
				data: newUser
			}
		} else {
			return {
				code: 300,
				msg: '当前用户已存在'
			};
		}
	}
	//查询用户
	async findOne(data) {
		const user = await this.ctx.model.Member.findOne({
			where: data,
			attributes:{
				exclude:['password']
			}
		})
		return user;
	}

	//修改用户数据
	async update(data) {
		const user = await this.ctx.model.Member.update(
			data,
			{
				where:{
					id:data.id
				},
			}
		)
		return user;
	}
}

module.exports = Members;